﻿using System;
using System.Windows.Forms;
using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Property;

namespace Medicalsystem.MCP.FormDesigner.MyControls
{
    /// <summary>
    /// 控件 MyDataGrid 属性的XML转换
    /// </summary>
    public class MyDataGridPropertyXML : CtrlUIPropertyXML
    {
        /// <summary>
        /// 控件 MultiTextBox 属性的XML转换
        /// </summary>
        /// <param name="panelCtl"></param>
        public MyDataGridPropertyXML(MyDataGrid myDataGrid)
            : base(myDataGrid)
        {
            _myDataGrid = myDataGrid;
        }

        private MyDataGrid _myDataGrid = null;

        public override string ToXML()
        {
            return "<AutoSizeColumnsMode>" + ((int)_myDataGrid.AutoSizeColumnsMode).ToString() + "</AutoSizeColumnsMode>" +
                       "<RowHeadersVisible>" + (_myDataGrid.RowHeadersVisible ? "1" : "0") + "</RowHeadersVisible>" +
                       "<Columns>" + GetMyGridColumnsXML() + "</Columns>" +
                       "<Base>" + base.ToXML() + "</Base>";
        }

        public override void FromXML(System.Xml.XmlNodeList xns)
        {
            if (xns != null && xns.Count > 0)
            {
                foreach (System.Xml.XmlNode XN in xns)
                {
                    switch (XN.Name.ToLower())
                    {
                        case "autosizecolumnsmode":
                            _myDataGrid.AutoSizeColumnsMode = (DataGridViewAutoSizeColumnsMode)Convert.ToInt32(XN.InnerText);
                            break;

                        case "rowheadersvisible":
                            _myDataGrid.RowHeadersVisible = base.StringToBool(XN.InnerText);
                            break;

                        case "columns":
                            _myDataGrid.MyColumns.Clear();
                            foreach (System.Xml.XmlNode node in XN.ChildNodes)
                            {
                                MyDataGridColumn col = new MyDataGridColumn();
                                foreach (System.Xml.XmlNode cnode in node.ChildNodes)
                                {
                                    switch (cnode.Name.ToLower())
                                    {
                                        case "headertext":
                                            col.HeaderText = ESCEncrypt.unescape(cnode.InnerText);
                                            break;

                                        case "width":
                                            col.Width = Convert.ToInt32(cnode.InnerText);
                                            break;
                                        default:
                                            break;
                                    }
                                }

                                _myDataGrid.MyColumns.Add(col);
                            }
                            _myDataGrid.InitColumns();
                            break;

                        case "base":
                            base.FromXML(XN.ChildNodes);
                            break;

                        default:
                            break;
                    }
                }
            }
        }

        private string GetMyGridColumnsXML()
        {
            string addXml = string.Empty;

            if (_myDataGrid.MyColumns != null)
            {
                foreach (MyDataGridColumn col in _myDataGrid.MyColumns)
                {
                    addXml += "<Column>" +
                              "<HeaderText>" + ESCEncrypt.escape(col.HeaderText) + "</HeaderText>" +
                              "<Width>" + col.Width.ToString() + "</Width>" +
                              "</Column>";
                }
            }

            return addXml;
        }
    }
}